<script setup>
  import WeatherTable from '@/components/WeatherTable.vue'
  import WeatherCard from '@/components/WeatherCard.vue'
  import { ref, onMounted, computed } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import weatherIcon from '@/composables/weatherIcon'
  import {
    getCityWeatherApi,
    getProvinceWeekWeatherApi,
    getProvinceADatyWeatherApi,
    getProvinceDetailWeatherApi
  } from '@/apis/weather'

  const router = useRouter()
  const route = useRoute()

  // 省详细天气情况
  const detailWeather = ref({})
  const getProvincedetail = async () => {
    const res = await getProvinceDetailWeatherApi(route.params.area)
    detailWeather.value = res.data.data
  }
  onMounted(() => {
    getProvincedetail()
  })

  // 省24小时天气情况
  const hourWeather = ref([])
  const getProvinceHour = async () => {
    const res = await getProvinceADatyWeatherApi(route.params.area)
    hourWeather.value = res.data.data
  }
  onMounted(() => {
    getProvinceHour()
  })
  // 省周天气情况
  const weekWeather = ref([])
  const getProvinceWeek = async () => {
    const res = await getProvinceWeekWeatherApi(route.params.area)
    weekWeather.value = res.data.data
  }
  onMounted(() => {
    getProvinceWeek()
  })

  // 子组城市件标题
  const provinceData = ref({
    title: '城市天气'
  })
  // 获取省下面城市天气
  const cityWeatherData = ref([])
  const getCityWeatherData = async () => {
    const res = await getCityWeatherApi(route.params.area)
    cityWeatherData.value = res.data.data
  }
  onMounted(() => {
    getCityWeatherData()
  })
  // 城市天气数据从新拼接图标字段
  const cityWeatherList = computed(() => {
    return cityWeatherData.value.map(item => ({
      ...item,
      ...weatherIcon[item.weatherCode]
    }))
  })
  // 点击跳转
  const onSkip = city => {
    router.push({
      path: '/city',
      query: {
        province: route.params.area,
        city: city
      }
    })
  }
</script>

<template>
  <div class="wrapper">
    <!-- 天气表格卡片 -->
    <WeatherTable
      :detailWeather="detailWeather"
      :hourWeather="hourWeather"
      :weekWeather="weekWeather"
    />
    <!-- 城市天气卡片 -->
    <WeatherCard :title="provinceData">
      <div
        v-for="(city, index) in cityWeatherList"
        :key="index"
        class="weather-card"
        @click="onSkip(city.city)"
      >
        <div class="city">{{ city.city }}</div>
        <div class="weather-content">
          <el-icon
            :size="24"
            class="weather-icon"
            :style="{ color: city.color }"
          >
            <component :is="city.icon" theme="outline" />
          </el-icon>
          <div class="weather-info">
            <span>{{ city.weather }}</span>
            <span>{{ city.temp}}℃</span>
          </div>
        </div>
      </div>
    </WeatherCard>
  </div>
</template>

<style scoped>
  .wrapper {
    margin: 0 auto;
    width: 1200px;
  }
  /* 天气卡片 */
  .weather-card {
    background: white;
    border-radius: 6px;
    padding: 12px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: all 0.3s;
  }

  .weather-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  .city {
    font-weight: 600;
    font-size: 14px;
    color: #303133;
    margin-bottom: 8px;
  }

  .weather-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }

  .weather-icon {
    margin-right: 6px;
  }

  .weather-info {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }

  .weather-info span:first-child {
    font-size: 13px;
    color: #606266;
  }

  .weather-info span:last-child {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
  }
</style>
